1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.collect.testing.MapInterfaceTest;
21  
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.Map;
25  
26  /**
27   * Test {@code TreeMultimap.asMap().subMap()} with {@link MapInterfaceTest}.
28   *
29   * @author Jared Levy
30   */
31  @GwtCompatible
32  public class SubMapMultimapAsMapImplementsMapTest
33      extends AbstractMultimapAsMapImplementsMapTest {
34  
35    public SubMapMultimapAsMapImplementsMapTest() {
36      super(true, true, true);
37    }
38  
39    private TreeMultimap<String, Integer> createMultimap() {
40      TreeMultimap<String, Integer> multimap
41          = TreeMultimap.create(Ordering.<String>natural().nullsFirst(),
42              Ordering.<Integer>natural().nullsFirst());
43      multimap.put("a", -1);
44      multimap.put("a", -3);
45      multimap.put("z", -2);
46      return multimap;
47    }
48  
49    @Override protected Map<String, Collection<Integer>> makeEmptyMap() {
50      return createMultimap().asMap().subMap("e", "p");
51    }
52  
53    @Override protected Map<String, Collection<Integer>> makePopulatedMap() {
54      TreeMultimap<String, Integer> multimap = createMultimap();
55      multimap.put("f", 1);
56      multimap.put("f", 2);
57      multimap.put("g", 3);
58      multimap.put("h", 4);
59      return multimap.asMap().subMap("e", "p");
60    }
61  
62    @Override protected String getKeyNotInPopulatedMap() {
63      return "a";
64    }
65  
66    @Override protected Collection<Integer> getValueNotInPopulatedMap() {
67      return Collections.singleton(-2);
68    }
69  
70    @Override public void testEntrySetRemoveAllNullFromEmpty() {
71      try {
72        super.testEntrySetRemoveAllNullFromEmpty();
73      } catch (RuntimeException tolerated) {
74        // GWT's TreeMap.entrySet().removeAll(null) doesn't throws NPE.
75      }
76    }
77  
78    @Override public void testEntrySetRetainAllNullFromEmpty() {
79      try {
80        super.testEntrySetRetainAllNullFromEmpty();
81      } catch (RuntimeException tolerated) {
82        // GWT's TreeMap.entrySet().retainAll(null) doesn't throws NPE.
83      }
84    }
85  
86    @Override public void testKeySetRemoveAllNullFromEmpty() {
87      try {
88        super.testKeySetRemoveAllNullFromEmpty();
89      } catch (RuntimeException tolerated) {
90        // GWT's TreeMap.keySet().removeAll(null) doesn't throws NPE.
91      }
92    }
93  
94    @Override public void testKeySetRetainAllNullFromEmpty() {
95      try {
96        super.testKeySetRetainAllNullFromEmpty();
97      } catch (RuntimeException tolerated) {
98        // GWT's TreeMap.keySet().retainAll(null) doesn't throws NPE.
99      }
100   }
101 
102   @Override public void testValuesRemoveAllNullFromEmpty() {
103     try {
104       super.testValuesRemoveAllNullFromEmpty();
105     } catch (RuntimeException tolerated) {
106       // GWT's TreeMap.values().removeAll(null) doesn't throws NPE.
107     }
108   }
109 
110   @Override public void testValuesRetainAllNullFromEmpty() {
111     try {
112       super.testValuesRemoveAllNullFromEmpty();
113     } catch (RuntimeException tolerated) {
114       // GWT's TreeMap.values().retainAll(null) doesn't throws NPE.
115     }
116   }
117 }